import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/',
      name: 'home',
      component: HomeView,
      redirect: '/welcome',
      children:[
        {
          path: '/welcome',
          name: 'welcome',
          component: () => import('../views/Welcome.vue')
        },
        {
          path: '/user_list',
          name: 'user_list',
          component: () => import('../views/UserView.vue')
        },
        {
          path: '/role_list',
          name: 'role_list',
          component: () => import('../views/MenuView.vue')
        },
        {
          path: '/author_list',
          name: 'author_list',
          component: () => import('../views/AuthorView.vue')
        },
        {
          path: '/group_list',
          name: 'group_list',
          component: () => import('../views/GroupView.vue')
        },
        {
          path: '/attribute_list',
          name: 'attribute_list',
          component: () => import('../views/AttributeView.vue')
        }
      ]
    }
  ]
})

export default router

// 做router跳转验证
router.beforeEach((to, from, next) => {
  if (to.path == '/login'){
    next()
  }else{
    // 获取token
    const token = sessionStorage.getItem('token')
    if (token == null || token == ''){
      next('/login')
    }
    next()
  }
})